package cn.cloudplug.instar_ec.main.launch;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import cn.cloudplug.core.index.ConfigKeys;
import cn.cloudplug.core.index.Scent;
import cn.cloudplug.core.net.RestClient;
import cn.cloudplug.core.net.callback.IError;
import cn.cloudplug.core.net.callback.IFailure;
import cn.cloudplug.core.net.callback.ISuccess;
import cn.cloudplug.core.util.SNUtil;
import cn.cloudplug.core.util.ScentPreference;
import cn.cloudplug.instar_ec.main.GlobalObjectKeys;
import cn.cloudplug.instar_ec.main.base.BaseProvider;
import cn.cloudplug.instar_ec.main.index.IndexDelegate;
import cn.cloudplug.instar_ec.main.login.LoginDelegate;
import cn.cloudplug.zdx_core.api.Common;
import cn.cloudplug.zdx_core.model.UserModel;
import retrofit2.Call;

/**
 * Created by pc on 2018/2/3.
 */

public class LaunchProvider extends BaseProvider implements ISuccess, IFailure, IError,Runnable{
    private static final long DELAYED_TIME = 3000;
    private String LAUNCH_PIC = "LAUNCH_PIC";

    private LaunchProvider(LaunchDelegate launchDelegate){
        this.mLaunchDelegate=launchDelegate;
        String sn = getDevicesSn();
        RestClient.builder()
                .url(Common.SOFTWARELOADING_URL)
                .params("serialno",sn)
                .success(this)
                .failure(this)
                .error(this)
                .build()
                .get();
    }

    public static LaunchProvider create(LaunchDelegate launchDelegate){
        return new LaunchProvider(launchDelegate);
    }

    public void onDelegateBindView(){
        Scent.put(GlobalObjectKeys.Activity,mLaunchDelegate.getActivity());
        Scent.mianHandler().postDelayed(this,DELAYED_TIME);
    }

    protected LaunchDelegate mLaunchDelegate;


    public String getDevicesSn() {

        String sn= ScentPreference.getCustomAppProfile("serialno");
        if(sn==null || "".equals(sn)){
            sn = SNUtil.getDevicesSn();
            ScentPreference.addCustomAppProfile("serialno",sn);
        }
        return sn;
    }


    @Override
    public void onSuccess(String url) {
        url= Scent.getConfiguration(ConfigKeys.BaseUrl).toString()+url;
        ScentPreference.addCustomAppProfile(LAUNCH_PIC,url);
        mLaunchDelegate.setImage(Glide.with(mLaunchDelegate)
                .load(url).apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)));
    }

    @Override
    public void onError(int code, String message) {
        load();
    }

    @Override
    public void onFailure(Call<?> call, Throwable t) {
        load();
    }

    @Override
    public void run() {
        if (UserModel.getInstance().isUserLogIn()) {
            mLaunchDelegate.getSupportDelegate().replaceFragment(new IndexDelegate(), false);
        } else
            mLaunchDelegate.getSupportDelegate().replaceFragment(new LoginDelegate(),false);
    }

    private void load() {
        String url = ScentPreference.getCustomAppProfile(LAUNCH_PIC);
        if(url!=null&&!"".equals(url)){
            mLaunchDelegate.setImage(Glide.with(mLaunchDelegate)
                    .load(url).apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL)));
        }
    }
}
